<template>
  <div class="student-container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list">
      <template v-for="item in message">
        <!-- 左侧是机器人小智 -->
        <div
          class="chat-item left"
          :key="item.timestamp"
          v-if="item.isStu === true"
        >
          <van-image
            fit="cover"
            round
            src="https://img1.baidu.com/it/u=3542758567,2075380174&fm=26&fmt=auto&gp=0.jpg"
          />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>

        <!-- 右侧是当前用户 -->
        <div class="chat-item right" :key="item.timestamp" v-else>
          <div class="chat-pao my">{{ item.msg }}</div>
          <van-image
            fit="cover"
            round
            src="https://img0.baidu.com/it/u=3144486528,3918230326&fm=26&fmt=auto&gp=0.jpg"
          />
        </div>
      </template>
    </div>
    <div class="reply-container van-hairline--top">
      <van-search
        v-model.trim="word"
        placeholder="说点什么..."
        @search="send"
        :autofocus="isfocus"
      >
      </van-search>
    </div>
  </div>
</template>

<script>
// 导入精美的图片
// import littleQ from '../../assets/logo.png'
// import self from '../../assets/logo.png'
// socket.io的客户端
import io from 'socket.io-client'
// import io from 'socket.io'
export default {
  name: 'student',
  data () {
    return {
      word: '',
      message: [], // 聊天信息
      //   littleQ, // 小智同学
      //   self, // 自己
      socket: undefined, // socket对象,
      isfocus: true
    }
  },
  created () {
    // 创建连接 并保存
    this.socket = io('http://118.24.62.36:8888')
    // 注册连接回调函数
    this.socket.on('connect', () => {
      console.log('建立连接成功了')
    })

    // 接收服务器的消息 小智
    this.socket.on('message', msg => {
      console.log('接收到消息了')
      console.log('msg:', msg)
      this.message.push({
        ...msg,
        // 是小智
        isStu: true
      })
    })
  },
  methods: {
    send () {
      console.log(this.word)
      this.isfocus = false
      if (this.word !== '') {
        this.socket.emit('message', {
          // 消息
          msg: this.word,
          // 时间戳
          timestamp: Date.now()
        })
        // 本地数组也要更新
        this.message.push({
          msg: this.word,
          // 时间戳
          timestamp: Date.now(),
          // 不是小智
          isStu: false
        })
      }
      this.word = ''
      this.$nextTick(() => {
        this.isfocus = true
      })
    }
  }
}
</script>

<style lang="less">
.student-container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon {
      color: white;
    }
  }
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
      .chat-pao.my {
        background-color: #9eea6a;
        &::before {
          content: '';
          background: #9eea6a;
        }
      }
    }
  }
  .chat-item.right {
    text-align: right;
    .chat-pao {
      margin-left: 0;
      margin-right: 15px;
      &::before {
        right: -6px;
        transform: rotate(45deg);
      }
    }
  }
  .chat-item.left {
    text-align: left;
    .chat-pao {
      margin-left: 15px;
      margin-right: 0;
      &::before {
        left: -5px;
        transform: rotate(-135deg);
      }
    }
  }
  .reply-container {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 44px;
    width: 100%;
    background: #f5f5f5;
    z-index: 9999;
  }
}
</style>
